﻿using AutoMapper;
using MediatR;
using Traceability.App.Applications.Command.TraceabilityManagement;
using Traceability.Domain.Entity;
using Traceability.Domain.Repository;
using Traceability.Enum;

namespace Traceability.App.Applications.CommandHand.TraceabilityManagementmodel
{
    public class DelListTraceabilityCommandHandler : IRequestHandler<DelListTraceabilityCommand, ApiResult<int>>
    {

        private readonly IBaseRepository<TraceabilityManagement> _traceab;
        private readonly IMapper mapper;

        public DelListTraceabilityCommandHandler(IBaseRepository<TraceabilityManagement> traceab, IMapper mapper)
        {
            _traceab = traceab;
            this.mapper = mapper;
        }

        public Task<ApiResult<int>> Handle(DelListTraceabilityCommand request, CancellationToken cancellationToken)
        {
            var result=new ApiResult<int>();
            var listid = request.Ids.Split(',').Select(x => Convert.ToInt32(x)).ToList();
            foreach (var item in listid)
            {

                var trace = _traceab.GetAll().FirstOrDefault(x => x.id == item);
                if (trace != null)
                {
                    _traceab.Delect(trace);
                }
              

            }
            result.Message = "批量删除完成";
            result.data = 1;
            return Task.FromResult(result);

        }
    }
}
